//
//  ST_BTMomentsCell.swift
//  btvideo
//
//  Created by mac on 2020/1/16.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit
import NIMSDK
import SwiftyUserDefaults
import SDWebImage

class ST_BTMomentsCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.backgroundColor = Color.white
        
        
        contentView.addSubview(avatar_image_view)
        avatar_image_view.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview().offset(20)
            make.width.height.equalTo(50)
        }
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(push_user_info))
        
        avatar_image_view.isUserInteractionEnabled  = true
        
        avatar_image_view.addGestureRecognizer(tap)
        
        
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(avatar_image_view.snp.right).offset(12)
            make.top.equalTo(avatar_image_view).offset(7)
        }
        contentView.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.left.equalTo(titleLabel)
            make.bottom.equalTo(avatar_image_view).offset(-7)
        }
        contentView.addSubview(tagButton)
        tagButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(content_label)
            make.left.equalTo(content_label.snp.right).offset(3)
        }
        
        let moreButtonW = 13.cgFloat
        
        contentView.addSubview(more_button)
        more_button.snp.makeConstraints { (make) in
            make.centerY.equalTo(avatar_image_view).offset(-5)
            make.right.equalToSuperview().offset(-20+(44-moreButtonW)/2.0)
            make.width.height.equalTo(44)
        }
        
        contentView.addSubview(cover_image_view)
        cover_image_view.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.equalTo(avatar_image_view.snp.bottom).offset(10)
            make.bottom.equalToSuperview().offset(-50)
        }
        
        contentView.addSubview(address_icon_image_view)
        contentView.addSubview(addressLabel)
        address_icon_image_view.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.bottom.equalToSuperview().offset(-26)
            make.width.equalTo(12)
            make.height.equalTo(14)
        }
        addressLabel.snp.makeConstraints { (make) in
            make.left.equalTo(address_icon_image_view.snp.right).offset(5)
            make.centerY.equalTo(address_icon_image_view)
        }
        
        
        contentView.addSubview(time_icon_image_view)
        time_icon_image_view.snp.makeConstraints { (make) in
            make.left.equalTo(addressLabel.snp.right).offset(20)
            make.centerY.equalTo(addressLabel)
            make.width.equalTo(14)
            make.height.equalTo(14)
        }
        contentView.addSubview(time_label)
        time_label.snp.makeConstraints { (make) in
            make.left.equalTo(time_icon_image_view.snp.right).offset(5)
            make.centerY.equalTo(time_icon_image_view)
        }
        
        
        
        contentView.addSubview(thumbUpBtn)
        thumbUpBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(cover_image_view.snp.bottom).offset(10)
            make.width.height.equalTo(70)
            make.right.equalToSuperview().offset(-20)
        }
        
        
        
        contentView.cornerRadius = 20
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //    MARK: - Properties
    fileprivate lazy var avatar_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.cornerRadius = 25
        return control
    }()
    
    fileprivate lazy var cover_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.cornerRadius = 20
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
//        control.backgroundColor = .random
        //        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 17, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = "添加特质"
        control.textColor = Color.init(hexString: "#ACB4C0")
//        control.backgroundColor = .random
        //        control.numberOfLines = 0
        //        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.medium)
        return control
    }()
    
    
   
    
    
    fileprivate lazy var tagButton:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
//        control.setImageForAllStates(ST_ImageUtils.imageName("消息_icon_发配对消息")!)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.semibold)
        control.setTitleColor(Color.init(hexString: "#FEC207")!, for: UIControl.State.normal)
        control.add_touch_up(insideTarget: self, action: #selector(push_peculiarity_handle))
        return control
    }()
    
    fileprivate lazy var more_button:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImageForAllStates(ST_ImageUtils.imageName("首页_icon_更多操作")!)
        control.add_touch_up(insideTarget: self, action: #selector(more_btn_click_handle))
//        control.backgroundColor = Color.random
        return control
    }()
        
    
    fileprivate lazy var time_icon_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.image = ST_ImageUtils.imageName("全局_icon_时间")
        control.clipsToBounds = true
        return control
    }()
    
    fileprivate lazy var time_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#ACB4C0")
        //        control.numberOfLines = 0
//        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.medium)
        return control
    }()
    
    fileprivate lazy var address_icon_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.clipsToBounds = true
        control.image = ST_ImageUtils.imageName("全局_icon_位置")
        return control
    }()
    
    fileprivate lazy var addressLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#ACB4C0")
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.medium)
        return control
    }()
    
    fileprivate lazy var thumbUpBtn:UIButton = {
       let control = UIButton.init(type: UIButton.ButtonType.custom)
       control.setImageForAllStates(ST_ImageUtils.imageName("首页_icon_赞dis")!)
       control.set_back_image_for_all_states(ST_ImageUtils.imageName("首页_bg_点赞")!)
        control.imageEdgeInsets = UIEdgeInsets.init(top: -10, left: 0, bottom: 0, right: 0)
       control.setImage(ST_ImageUtils.imageName("首页_icon_赞sel"), for: UIControl.State.selected)
       control.add_touch_up(insideTarget: self, action: #selector(thumb_up_handle))
       return control
   }()
    
    var ignore_somebody_closure:((_ userId:String,_ noticeText:String)->())?
    
    var unpair:((_ userId:String)->())?
    //    MARK:-
    var model = ST_BTMomentsModel(){
        didSet{
            
            
            
            
            avatar_image_view.pin_setImage(from: model.avatar.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            cover_image_view.pin_setImage(from: model.content.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            titleLabel.text = model.nickname
            
            addressLabel.text = model.address_more
            
            time_label.text = model.create_time.b_t_global_date_string()
            
            tagButton.setTitleForAllStates(model.feature_name)
            
            tagButton.isHidden = model.feature_name.isEmpty
            
            thumbUpBtn.isSelected = model.thumb_type == 1
        }
    }
    
}

//MARK: - Event

extension ST_BTMomentsCell{
    
    @objc func push_peculiarity_handle(){
        
        push_peculiarity(id: model.feature_id, onlyDisplayPerson: false, navigationController: self.parentViewController?.navigationController)
        
    }
    
    @objc func more_btn_click_handle(){
        
        let vc = ST_BTSheetActionController.init(title: nil, message: nil)
        
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium),.foregroundColor: UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)]
        
      
        if ST_BTUserInfoManager.shared.current_account_info?.userinfo?.sex == .male || Defaults[.isPanda] {
        
        let str2 = NSMutableAttributedString.init(string: "拉黑")
        
        str2.addAttributes(attr, range: NSRange(location: 0, length: str2.length))
        
        vc.addAction(ST_BTAlertAction.init(title: str2, handler: { (action) in
            
            self.ban_current_user_handle()
        }))
        }
        
        let str3 = NSMutableAttributedString.init(string: "举报")
        
        str3.addAttributes(attr, range: NSRange(location: 0, length: str3.length))
        
        vc.addAction(ST_BTAlertAction.init(title: str3, handler: { (action) in
           
            let vc = ST_ReportPersonController()
            vc.userId = self.model.accid
            let nav = UINavigationController.create_hiding_bar_navigation_controller(rootViewController: vc)
            GlobalPresent(toController: nav)
            
        }))
        
        vc.show()
        
    }
    
    @objc func thumb_up_handle(){
        
        if thumbUpBtn.isSelected{
            
            let thumbUpHud = ST_BTHUDController.init(noticeText: "你已经点过赞啦")
            
            GlobalPresent(toController: thumbUpHud, animated: false)
            
            main_thread_execute(afterDelay: 2.5) {
                thumbUpHud.dismiss(animated: false, completion: nil)
            }
            
            return
        }
       
        guard let img = cover_image_view.image else { return }
        
        self.send_thumb_up_msg(image: img)
    }
    
    fileprivate func send_thumb_up_msg(image:UIImage){
        
        self.parentViewController?.show_indeterminate_h_u_d()
      
        
//        let words = "我点赞了你的特质 content = \(self.model.content)"
        
        
        JVrequest(PH.thumbUpApi,parameters:["target_accid":model.accid,"feature_content_id":model.feature_content_id] ,success: { (json) in
        
            self.parentViewController?.hidden_indeterminate_h_u_d()
            
            self.thumbUpBtn.isSelected = !self.thumbUpBtn.isSelected
            
            self.model.thumb_type = 1
            
            let msg = NIMMessage()
            
            msg.remoteExt = ["thumbUp":"1"]
            
            let img = NIMImageObject.init(image: image)
            
            msg.messageObject = img
                
            ST_DTChatMessageSendHander.shared.send_to_n_i_m(msg: msg, sessionId: self.model.accid)
            
        }) { (code, err) in
            
            guard let v = self.parentViewController?.view else{ return }
            
            ToastHud(text: err, view: v)
        }
        
        
    }
    
    
    @objc func push_user_info(){
        
        let vc = ST_PairsCenterViewController()
                
        vc.accid = self.model.accid
        
        let nav = PairsCenterInteractiveNavigationController.init(rootViewController: vc)
        
        nav.overCurrentContext = true
        
        GlobalPresent(toController: nav)
        
    }
    
    @objc func ban_current_user_handle(){
        
        
        BAN_SOMEBODY_HANDLE(userId: model.accid, close_view: false, hud_controller: self.parentViewController)
        
    }
    
}
